<?php
class CollectcategoryAction extends CommonAction {
	function index(){
		$this->listmap = 1;
		$this->listmap.=" and pid=0";
        parent::_list();
		$Category=D('Collectcategory');
		$list=$this->list;
		foreach($list as $k=>$v){
			$list[$k]['sub']=$Category->getListToArray1($v['id']);
		}
		$this->assign('list',$list);
		$this->display();
	}
	
	public function delete(){
		if($this->_get('id')){
			$map['pid']=$this->_get('id');
			$re=$this->dao->where($map)->find();
			if($re){
				$this->error('该分类包含了子类,请先删除子类');
			}else{
				$all=$this->dao->where('id in('.$this->_get('id').')')->select();
				foreach($all as $k=>$v){
					//删除图片
					$this->delImgBypath($v['img_url']);
				}
				parent::delete();
			}
		}
	}
	public function add(){
		$pid_list=$this->dao->where(array('pid'=>0))->select();
		$category=D('Collectcategory');
		foreach($pid_list as $k=>$v){
			$pid_list[$k]['sub']=$category->getListToArray1($v['id'],$this->siteid);
		}
		$this->assign('pid_list',$pid_list);
		if($this->_get('id')){
			$this->assign('id',$this->_get('id'));
		}
		parent::add();
	}
	

	public function edit(){
		$pid_list=$this->dao->where(array('pid'=>0))->select();
		$category=D('Collectcategory');
		foreach($pid_list as $k=>$v){
			$pid_list[$k]['sub']=$category->getListToArray1($v['id']);
		}
		$this->assign('pid_list',$pid_list);
		parent::edit();
	}
	
	//重写更新方法,防止用户选择自己作为父分类
	public function update(){
		if($this->_post('pid')==$this->_post('id')){
			$this->error('您不能选择当前分类作为父类');
		}else{
			parent::update();
		}
	}
	
}
